package cn.nawang.ebeim.client.version;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DirectoryNode implements Node {

    private static final long serialVersionUID = 2L;

    private String name;

    private String dsId;

    private Node parent;

    private byte status = STATUS_NOOP;

    private long id;

    private long pId;

    private String path;

    private boolean hasRefresh;

    private boolean open ;

    public boolean getOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public void setPath(String path) {
        this.path = path;
    }

    private Map<String, Node> children = new HashMap<String, Node>();

    private String iconSkin = "sty03" ;

    public DirectoryNode() {
    }

    public DirectoryNode(String name) {
        this.name = name;
    }

    @JSONField(serialize = false)
    public Map<String, Node> getChildren() {
        return children;
    }

    public long getId() {
        return id;
    }

    public String getIconSkin() {
        return iconSkin;
    }

    public void setIconSkin(String iconSkin) {
        this.iconSkin = iconSkin;
    }

    public String getDsId() {
        return dsId;
    }

    public void setPId(long pId) {
        this.pId = pId;
    }

    public long getPId() {
        return pId;
    }

    public void setDsId(String dsId) {
        this.dsId = dsId;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setChildren(Map<String, Node> children) {
        this.children = children;
    }

    @JSONField(serialize = false)
    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public byte getStatus() {
        return status;
    }


    public void setStatus(byte status) {
        this.status = status;
    }

    public boolean filterChanges(List<Node> list) {
        boolean flag = false;
        for (Node n : children.values()) {
            if (n.filterChanges(list)) {
                flag = true;
            }
        }
        return flag;
    }

    public void addNode(Node node) {
        children.put(node.getName(), node);
        node.setParent(this);
    }

    public VersionTreeInfo versionTreeInfo(VersionTreeInfo versionTreeInfo) {
        for (String s : children.keySet()) {
            children.get(s).versionTreeInfo(versionTreeInfo);
        }
        versionTreeInfo.dirNodes.add(this);
        return versionTreeInfo;
    }

    public void refresh() {
        if (hasRefresh) {
            return;
        }
        hasRefresh = true;
        File dir = new File(path);
        File[] files = dir.listFiles();
        for (File file : files) {
            Node node = children.get(file.getName());
            if (node != null) {
                node.setStatus(STATUS_NO_CHANGE);
                node.refresh();
            } else {
                VersionTree versionTree = new VersionTree(this);
                versionTree.generateVersionTree(file.getPath());
            }
        }
        for (String s : children.keySet()) {
            Node node = children.get(s);
            node.refresh();
        }

    }

    @Override
    public String toString() {
        return "DirectoryNode{" +
                "name='" + name + '\'' +
                ", dsId='" + dsId + '\'' +
                ", parent=" + parent +
                ", status=" + status +
                ", id=" + id +
                ", path='" + path + '\'' +
                '}';
    }
}
